package com.elai.operate.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elai.analytics.service.impl.TouchModeServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.operate.domain.OperatePushConfig;
import com.elai.operate.service.IOperatePushConfigService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 推送通道配置Controller
 *
 * @author elai
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/operate/pushconfig")
public class OperatePushConfigController extends BaseController
{
    @Autowired
    private IOperatePushConfigService operatePushConfigService;
    @Autowired
    private TouchModeServiceImpl touchModeService;

    /**
     * 查询推送通道配置列表
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(OperatePushConfig operatePushConfig)
    {
        startPage();
        List<OperatePushConfig> list = operatePushConfigService.selectOperatePushConfigList(operatePushConfig);
        return getDataTable(list);
    }

    @GetMapping("/getPushConfigListGroup")
    public AjaxResult getPushConfigListGroup(OperatePushConfig operatePushConfig)
    {
        List<OperatePushConfig> list = operatePushConfigService.selectOperatePushConfigList(operatePushConfig);
        Map<String, List<OperatePushConfig>> followUserData = list.stream().collect(Collectors.groupingBy(OperatePushConfig::getChannelServiceCode));
        return AjaxResult.success(followUserData);
    }

    /**
     * 导出推送通道配置列表
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:export')")
    @Log(title = "推送通道配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OperatePushConfig operatePushConfig)
    {
        List<OperatePushConfig> list = operatePushConfigService.selectOperatePushConfigList(operatePushConfig);
        ExcelUtil<OperatePushConfig> util = new ExcelUtil<OperatePushConfig>(OperatePushConfig.class);
        return util.exportExcel(list, "推送通道配置数据");
    }

    /**
     * 获取推送通道配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(operatePushConfigService.selectOperatePushConfigById(id));
    }

    /**
     * 新增推送通道配置
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:add')")
    @Log(title = "推送通道配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperatePushConfig operatePushConfig)
    {
        return toAjax(operatePushConfigService.insertOperatePushConfig(operatePushConfig));
    }

    /**
     * 修改推送通道配置
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:edit')")
    @Log(title = "推送通道配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperatePushConfig operatePushConfig)
    {
        return toAjax(operatePushConfigService.updateOperatePushConfig(operatePushConfig));
    }

    /**
     * 删除推送通道配置
     */
    @PreAuthorize("@ss.hasPermi('operate:pushconfig:remove')")
    @Log(title = "推送通道配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(operatePushConfigService.deleteOperatePushConfigByIds(ids));
    }


    @PostMapping("/testSend")
    public AjaxResult testSend(@RequestBody String requestJson)
    {
        System.out.println(requestJson);
        JSONObject jsonObject = JSON.parseObject(requestJson);
        Map<String,String> currentAccount = ( Map<String,String> )jsonObject.get("currentAccount");
        String channelType = currentAccount.get("channelType");
        if("PUSH".equals(channelType)){
            //推送类型
        }else if("TEXT_MSG".equals(channelType)) {
            JSONObject requestJson1 = JSON.parseObject((String) jsonObject.get("requestJson"));
            List<JSONObject> paramList = (List<JSONObject>) requestJson1.get("paramJson");
            String phone = null;
            for (JSONObject object : paramList) {
                if ("phone".equals(object.get("paramName"))) {
                    phone = (String) object.get("paramValue");
                }
            }
            //模板ID
            Long templateId = Long.valueOf((String) requestJson1.get("templateId"));
            //发送类型

//        Boolean templateId = touchModeService.sendMessage(Long.valueOf((String) requestJson1.get("templateId")), phone);
        }
        return AjaxResult.success();
    }
}
